<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//商品管理控制器
class GoodsController extends CommonController{
	private $model;
	public function __construct(){
		parent::__construct();
		$this->model = new \Common\Model\Goods;
	}
	
	
	//添加商品
	public function add(){
		if(IS_POST){
			if($this->model->addData()) View::success('添加成功',U('index'));
			View::error($this->model->getError());
		}
		
		//1.处理所属分类
		
		//实例化分类模型
		$cateModel = new \Common\Model\Cate;
		//让分类以树状形式显示
		$cateData = Data::tree($cateModel->get(),'cname');
		//分配
	    View::with('cateData',$cateData);
		
		View::make();
	}
	
	//显示商品列表(只显示商品的最新一期)
	public function index(){
		//实例化期数表
		$periodsModel = new \Common\Model\Periods;
		//得到商品表的所有数据
		$goodsData = $this->model->get();
//		sp($goodsData);
		//循环商品数据
		foreach ($goodsData as $k => $v) {
			//select * from goods_periods order by periods desc limit 1;
			//找到期数表里面的goods_gid等于商品gid的，并把periods字段倒序排列截取第一条，并以数组的形式列出来
			$periodsNums = $periodsModel->orderBy('periods','DESC')->where("goods_gid={$v['gid']}")->limit(1)->lists('periods');
//			sp($periodsNums);exit;
			if($periodsNums){
				//如果给商品添加过去期数
				$goodsData[$k]['newNum'] = $periodsNums[0];
			}else{
				//如果没有给商品添加过期数，默认给它插入第一期
				$goodsData[$k]['newNum'] = 1;
			}
		}
		View::with('data',$goodsData);
	    View::make();
	}
	//插件 uploadify异步上传
	public function upload(){
		$file = Upload::path('Upload/Content/' . date('y/m'))->make();
		if(empty($file)){
			// 相当于：echo json_encode(Upload::getError());exit;
			$this->ajax(Upload::getError());
		}else{
			 /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
			$data = $file[0];
			// 相当于：echo json_encode($data);exit;
			$this->ajax($data);
		}
	}
	
	//编辑商品
	public function edit(){
		if(IS_POST){
//			sp(Q('post.'));exit;
			if($this->model->editData()) View::success('修改成功',U('index'));
			View::error($this->model->getError());
		}
		
		
		//1.处理所属分类
		$cateModel = new \Common\Model\Cate;
		//让分类在页面上以树状的形式显示
		$cateData = Data::tree($cateModel->get(),'cname');
		View::with('cateData',$cateData);
		//2.获得旧数据
		$gid = Q('get.gid',0,'intval');
		$oldData = Db::table('goods')->join('goods_details','gid','=','goods_gid')->where("gid={$gid}")->get();
//		sp($oldData);exit;
		
		//打印出来的是二维数组，以后要注意这个问题
		View::with('oldData',$oldData[0]);
	    View::make();
		
	}
	
	//删除商品
	public function del(){
		$this->model->realDel(Q('get.gid',0,'intval'));
		View::success('删除成功');
	}
	
	//已经下架的商品
	public function out(){
		//获得要下架的哪一条
		$gid = Q('get.gid',0,'intavl');
		$this->model->where("gid={$gid}")->update(array("gstate"=>1,"out_time"=>time()));
		
		View::success('商品已下架',U('index'));
	}
	
	//下架商品列表
	public function soldOut(){
		$outData = $this->model->where("gstate=1")->get();
		View::with('outData',$outData);
		View::make();
	}
	
	//从下架商品列表还原到商品列表
	public function restore(){
		$gid = Q('get.gid',0,'intavl');
		$this->model->where("gid={$gid}")->update(array("gstate"=>0));
	    View::success('还原成功',U('soldOut'));
	}
}




 ?>